package com.qk.management.service.Impl;

import ch.qos.logback.core.util.Loader;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domin.business.BusinessDO;
import com.qk.domin.business.BusinessTrackRecordDO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.BusinessQueryDTO;
import com.qk.dto.business.BusinessUpdateDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.BusinessService;
import com.qk.management.util.CurrentUserContextHolders;
import com.qk.vo.business.BusinessVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 16778
 * @date 2025/10/14 19:58
 * @description
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private CustomerMapper customerMapper;


    @Override
    public void insert(Business business) {
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(business);
    }

    @Override
    public PageResult<BusinessDO> selectByPage(BusinessQueryDTO dto) {
        Page<BusinessDO> pageParam = new Page<>(dto.getPage(), dto.getPageSize());
        Page<BusinessDO> page = this.baseMapper.selectByPage(pageParam, dto);
        return PageResult.<BusinessDO>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
    }

    @Override
    public void assign(Integer businessId, Integer userId) {
        Business business = new Business();
        business.setId(businessId);
        business.setUpdateTime(LocalDateTime.now());
        business.setUserId(userId);
        business.setStatus(BusinessStatusConstants.WAIT_FOLLOW_UP);
        this.baseMapper.updateById(business);
    }

    @Override
    public BusinessVO getWithTrackRecordByBusinessId(Integer id) {
        Business business = this.baseMapper.selectById(id);
        BusinessVO vo = BeanUtil.copyProperties(business, BusinessVO.class);
        List<BusinessTrackRecordDO> trackRecords = this.baseMapper.getTrackRecordByBusinessId(id);
        vo.setTrackRecords(trackRecords);
        return vo;
    }

    @Override
    @Transactional
    public void updateWithBusinessTrackRecordByBusinessId(BusinessUpdateDTO dto) {
        Business business = BeanUtil.copyProperties(dto, Business.class);
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(BusinessStatusConstants.FOLLOW_UP);
        this.baseMapper.updateById(business);

        BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(dto, BusinessTrackRecord.class);
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecord.setKeyItems(dto.getKeyItems().toString());
        businessTrackRecord.setId(null);
        businessTrackRecord.setBusinessId(dto.getId());
        businessTrackRecord.setUserId(CurrentUserContextHolders.getCurrentUserId());
        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public void convertBackToFake(Integer id) {
        Business business = new Business();
        business.setId(id);
        business.setStatus(BusinessStatusConstants.BACKTOFAKE);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);
    }

    @Override
    @Transactional
    public void convertToCustomer(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setStatus(BusinessStatusConstants.CONVERT_TO_CUSTOMER);
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(business);

        Customer customer = BeanUtil.copyProperties(business, Customer.class);
        customer.setId(null);
        customer.setBusinessId(business.getId());
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> pool(BusinessPoolDTO dto) {
        dto.setStatus(BusinessStatusConstants.BACKTOFAKE);
        Page pageParam = new Page(dto.getPage(),dto.getPageSize());
        Page<Business> page = this.baseMapper.pool(pageParam,dto);
        return PageResult.<Business>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
    }
}
